package org.alex.samples.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.alex.samples.model.CommonMessageResultVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.SubjectAndTypes;
import org.alex.samples.service.IManageTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/teacher")
public class ManageTestController {

	@Autowired
	private IManageTestService manageTestService;
	
	
	//显示全部试题信息
	@RequestMapping(value="/toManageTest")
	public String toManageTest(Model model){
		List<ExamTestVO> testList=manageTestService.loadAllTest();
		model.addAttribute("testList",testList);
		return "teacher/manageTest";
	}
	
	//查看详细考题信息
	@ResponseBody
	@RequestMapping(value="/getTestById",method = RequestMethod.POST)
	public ExamTestVO getTestById(Integer testId){
		ExamTestVO resultVO=manageTestService.getTestById(testId);
		return resultVO;
	}
	


	//查看科目和试题类型
	@ResponseBody
	@RequestMapping(value="/getSubjectAndTestType")
	public SubjectAndTypes getSubjectAndTypes(){		
		return manageTestService.getSubjectAndTypes();
		
	}
	
	
//新增考题信息
@ResponseBody
@RequestMapping(value="/saveTest",method=RequestMethod.POST)
public CommonMessageResultVO saveTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
	CommonMessageResultVO resultVO=new CommonMessageResultVO();
	Map<String,String> messageMap=new HashMap<String,String>();
	resultVO.setSuccess(false);
	if(bindingResult.hasErrors()){
		List<ObjectError> errorList=bindingResult.getAllErrors();
		for(ObjectError error:errorList){
			FieldError fe=(FieldError) error;
			messageMap.put(fe.getField(),fe.getDefaultMessage());
		}
		resultVO.setMessageMap(messageMap);
		return resultVO;
	}
	
	try{
		manageTestService.addTest(examTestVO);
		resultVO.setSuccess(true);
	}catch(Exception e){
		e.printStackTrace();
		messageMap.put("message", e.getMessage());
		resultVO.setMessageMap(messageMap);
	}
	return resultVO;
}



//修改考题
@ResponseBody
@RequestMapping(value="/updateTest",method=RequestMethod.POST)
public CommonMessageResultVO updateTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
	CommonMessageResultVO resultVO=new CommonMessageResultVO();
	Map<String,String> messageMap=new HashMap<String,String>();
	resultVO.setSuccess(false);
	if(bindingResult.hasErrors()){
		List<ObjectError> errorList=bindingResult.getAllErrors();
		for(ObjectError error:errorList){
			FieldError fe=(FieldError) error;
			messageMap.put(fe.getField(),fe.getDefaultMessage());
		}
		resultVO.setMessageMap(messageMap);
		return resultVO;
	}
	
	try{
		manageTestService.editTest(examTestVO);
		resultVO.setSuccess(true);
	}catch(Exception e){
		e.printStackTrace();
		messageMap.put("message", e.getMessage());
		resultVO.setMessageMap(messageMap);
	}
	return resultVO;
}



//删除科目
	@ResponseBody
	@RequestMapping(value="/deleteTest",method=RequestMethod.POST)
	public CommonMessageResultVO deleteTest(Integer testId){
		CommonMessageResultVO resultVO=new CommonMessageResultVO();
		Map<String,String> messageMap=new HashMap<String,String>();
		resultVO.setSuccess(false);
		try{
			manageTestService.deleteTest(testId);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	

		
	
}
